/**
 * @file   RegistrationResponsePacket.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-21
 * @brief  DON packet object: Registration Response
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DON/Core/Packet/RegistrationResponsePacket.h>
#include <DON/Core/Packet/GenericPacket.h>
#include <DON/Core/DataType/UInt8.h>
#include <DON/Core/DataType/UInt16.h>
#include <DON/Core/DataType/Structure.h>
#include <QtCore/QByteArray>

using namespace DON::Core::Packet;
using namespace DON::Core::DataType;

class RegistrationResponsePacket::Private
{
public:
    Private()
        : requestSequence(0u),
          responseStatus(ResponseStatus_Invalid)
    { }

    quint16 requestSequence;
    ResponseStatus responseStatus;
};

RegistrationResponsePacket::RegistrationResponsePacket()
    : AbstractPacket(),
      d(new Private())
{
    setPacketType(PacketType_RegistrationResponse);
}

RegistrationResponsePacket::RegistrationResponsePacket(const quint16 source,
                                                       const quint16 destination,
                                                       const quint16 sequence,
                                                       const quint16 referenceSequence,
                                                       const ResponseStatus responseStatus)
    : AbstractPacket(source,
                     destination,
                     PacketType_RegistrationResponse,
                     sequence),
      d(new Private())
{
    d->requestSequence = referenceSequence;
    d->responseStatus = responseStatus;
}

RegistrationResponsePacket::RegistrationResponsePacket(const RegistrationResponsePacket &other)
    : AbstractPacket(other),
      d(new Private())
{
    d->requestSequence = other.d->requestSequence;
    d->responseStatus = other.d->responseStatus;
}

RegistrationResponsePacket::~RegistrationResponsePacket()
{
    delete d;
}

RegistrationResponsePacket & RegistrationResponsePacket::operator=(
        const RegistrationResponsePacket &other)
{
    AbstractPacket::operator=(other);

    d->requestSequence = other.d->requestSequence;
    d->responseStatus = other.d->responseStatus;

    return *this;
}

bool RegistrationResponsePacket::isValid() const
{
    const bool valid = isResponseStatusValid(d->responseStatus);

    return valid;
}

quint16 RegistrationResponsePacket::getRequestSequence() const
{
    return d->requestSequence;
}

void RegistrationResponsePacket::setRequestSequence(const quint16 requestSequence)
{
    d->requestSequence = requestSequence;
}

ResponseStatus RegistrationResponsePacket::getResponseStatus() const
{
    return d->responseStatus;
}

void RegistrationResponsePacket::setResponseStatus(const ResponseStatus responseStatus)
{
    d->responseStatus = responseStatus;
}

RegistrationResponsePacket RegistrationResponsePacket::fromGenericPacket(
        const GenericPacket &genericPacket)
{
    bool success = false;
    RegistrationResponsePacket packet;

    if (genericPacket.isValid() &&
        (genericPacket.getPacketType() == packet.getPacketType()))
    {
        // Parse payload items
        const Structure *payloadItems = genericPacket.getPayloadItems(&success);

        if (success)
        {
            if (payloadItems->getMemberCount() != 2)
            {
                success = false;
            }
        }

        // Request Sequence
        quint16 requestSequence = 0u;

        if (success)
        {
            const AbstractDataType *adt = payloadItems->getMember(0);
            const UInt16 *value = UInt16::fromAbstractDataType(adt);

            if (value != NULL)
            {
                requestSequence = value->getValue();
            }
            else
            {
                success = false;
            }
        }

        // Response Status
        ResponseStatus responseStatus = ResponseStatus_Invalid;

        if (success)
        {
            const AbstractDataType *adt = payloadItems->getMember(1);
            const UInt8 *value = UInt8::fromAbstractDataType(adt);

            if (value != NULL)
            {
                const char data = static_cast<char>(value->getValue());
                responseStatus = fromBinaryToResponseStatus(data);

                success = isResponseStatusValid(responseStatus);
            }
            else
            {
                success = false;
            }
        }

        delete payloadItems;
        payloadItems = NULL;

        // Save values
        if (success)
        {
            packet.setSource(genericPacket.getSource());
            packet.setDestination(genericPacket.getDestination());
            packet.setSequence(genericPacket.getSequence());

            packet.setRequestSequence(requestSequence);
            packet.setResponseStatus(responseStatus);
        }
    }

    return packet;
}

QList<QByteArray> RegistrationResponsePacket::getPayloadDataItems(bool *ok) const
{
    QList<QByteArray> payloadDataItems;
    bool success = false;

    if (isValid())
    {
        success = true;

        // Request Sequence
        payloadDataItems.append(UInt16::toBinary(d->requestSequence));

        // Response Status
        const char responseStatus = fromResponseStatusToBinary(d->responseStatus);
        payloadDataItems.append(QByteArray(1, responseStatus));
    }

    if (ok != NULL)
    {
        *ok = success;
    }

    return payloadDataItems;
}
